
#include "cybertron/core/UtilWin32.hpp"
#include "cybertron/core/UtilPath.hpp"
//#include "../PlatformHeader.hpp"

#ifdef CYBERTRON_WIN
#include <shlobj.h>
#include <WinUser.h>
#include <atlconv.h>
#include <windows.h>
#endif

CYBERTRON_BEGIN
UtilWin32::UtilWin32()
{
}

UtilWin32::~UtilWin32()
{
}
#ifdef CYBERTRON_WIN
CybertronCoreAPI std::string UtilWin32::WCharToString(LPCWSTR pwszSrc) {
	int len = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
	if (len <= 0) {
		return std::string("");
	}
	char *pszDst = new char[len];
	if (nullptr == pszDst) {
		return std::string("");
	}
	WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, len, NULL, NULL);
	pszDst[len - 1] = 0;
	std::string strTemp(pszDst);
	delete[]pszDst;
	return strTemp;
}

CybertronCoreAPI LPCWSTR UtilWin32::StringToLPCWSTR(std::string orig)
{
	size_t origsize = orig.length() + 1;
	const size_t newsize = 100;
	size_t convertedChars = 0;
	wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(orig.length() - 1));
	mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
	return wcstring;
}

CybertronCoreAPI bool UtilWin32::CreatLinkToStartMenu(std::string scriptPath, std::string scriptName){
	std::string pszPeFileName = UtilPath::combine(scriptPath, scriptName);
	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr)) {
		IShellLink *pisl;
		hr = CoCreateInstance(CLSID_ShellLink, NULL,
			CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);
		if (SUCCEEDED(hr)) {
			IPersistFile* pIPF;
			auto fileName = StringToLPCWSTR(pszPeFileName);
			pisl->SetPath(fileName);
			hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
			if (SUCCEEDED(hr)) {
				char szStartPath[MAX_PATH] = { 0 };
				WCHAR wszClassName[128];
				memset(wszClassName, 0, sizeof(wszClassName));
				MultiByteToWideChar(CP_ACP, 0, szStartPath, strlen(szStartPath) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));

				SHGetSpecialFolderPath(NULL, wszClassName, CSIDL_STARTUP, 0);
				std::string startPath = WCharToString(wszClassName);
				startPath = startPath +"\\"+ scriptName+".lnk";
				USES_CONVERSION;
				//LPCWSTR pwcStr = StringToLPCWSTR(startPath.c_str());
				//auto lpcStr = W2CA(pwcStr);
				//LPCOLESTR lpOleStr = A2COLE(lpcStr);
				WCHAR wsz[MAX_PATH] = { 0 };
				MultiByteToWideChar(CP_ACP, 0, startPath.c_str(), startPath.size(), wsz, startPath.size() * sizeof(wchar_t));
				pIPF->Save(wsz, TRUE);
				pIPF->Release();
			}
			pisl->Release();
		}
		CoUninitialize();
	}
	return true;
}
CybertronCoreAPI bool UtilWin32::CreatLinkToDeskTop(std::string scriptPath, std::string scriptName) {
	std::string pszPeFileName = UtilPath::combine(scriptPath, scriptName);
	HRESULT hr = CoInitialize(NULL);
	if (SUCCEEDED(hr)) {
		IShellLink *pisl;
		hr = CoCreateInstance(CLSID_ShellLink, NULL,
			CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pisl);
		if (SUCCEEDED(hr)) {
			IPersistFile* pIPF;
			auto fileName = StringToLPCWSTR(pszPeFileName);
			pisl->SetPath(fileName);
			hr = pisl->QueryInterface(IID_IPersistFile, (void**)&pIPF);
			if (SUCCEEDED(hr)) {
				char szStartPath[MAX_PATH] = { 0 };
				WCHAR wszClassName[128];
				memset(wszClassName, 0, sizeof(wszClassName));
				MultiByteToWideChar(CP_ACP, 0, szStartPath, strlen(szStartPath) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));

				SHGetSpecialFolderPath(NULL, wszClassName, CSIDL_DESKTOP, 0);
				std::string startPath = WCharToString(wszClassName);
				startPath = startPath + "\\" + scriptName + ".lnk";
				USES_CONVERSION;
				//LPCWSTR pwcStr = StringToLPCWSTR(startPath.c_str());
				//auto lpcStr = W2CA(pwcStr);
				//LPCOLESTR lpOleStr = A2COLE(lpcStr);
				WCHAR wsz[MAX_PATH] = {0};
				MultiByteToWideChar(CP_ACP, 0, startPath.c_str(), startPath.size(), wsz, startPath.size() * sizeof(wchar_t));
				pIPF->Save(wsz, FALSE);
				pIPF->Release();
			}
			pisl->Release();
		}
		CoUninitialize();
	}
	return true;
}
#endif
CYBERTRON_END
